'use strict';

import Base from './base.js';

export default class extends Base {
	async fenpeiAction(){
		let id = [1,2];
		let list = await this.model('or_rule_admin').where({id: ['IN', id]}).select();
		function time(unix){
			let tm = new Date(unix*1000);
			let yy = tm.getFullYear();
			let mm = tm.getMonth()<9?'0'+(tm.getMonth()+1):tm.getMonth()+1;
			let dd = tm.getDate()<10?'0'+tm.getDate():tm.getDate();
			let hh = tm.getHours()<10?'0'+tm.getHours():tm.getHours();
			let ii = tm.getMinutes()<10?'0'+tm.getMinutes():tm.getMinutes();
			let ss = tm.getSeconds()<10?'0'+tm.getSeconds():tm.getSeconds();
			return yy+'-'+mm+'-'+dd+' '+hh+':'+ii+':'+ss;
		}
		this.assign({'list': list, 'time': time});
		return this.display();
	}

	async setquanAction(){
		if (this.isPost) {
			let data = this.post();
			let time = Math.round(new Date().valueOf()/1000);
			let res = await this.model('or_rule_admin').where({id: data.id}).update({gradepermission: data.data, edittime: time});
			return res?this.success('分配成功'):this.fail('分配失败');
		} else {
			let id = this.get('id');
			let qinfo = await this.model('or_rule_admin').where({id: id}).find();

			let type = [];
			if (qinfo.id === 1) {
				type = [3,4];
			}
			if (qinfo.id === 2) {
				type = [5,6];
			}
			if (qinfo.id === 3) {
				type = [1,2];
			}
			let opts = await this.model('or_rulemodel').where({category: ['IN', type]}).select();

			let q_arr = JSON.parse(qinfo.gradepermission);

			this.assign({'qinfo': q_arr, 'opts': opts, 'id': id});
			return this.display();
		}
	}

	async editAction(){
		if (this.isPost) {
			let data = this.post();
			let time = Math.round(new Date().valueOf()/1000);

			let repeat = await this.model('or_rule_admin').where({remarks: data.remarks, departmentid: data.departmentid, id: ['!=', data.id]}).find();
			if (!think.isEmpty(repeat)) {
				return this.fail('等级描述信息重复');
			}

			let res = await this.model('or_rule_admin').where({id: data.id}).update({remarks: data.remarks, edittime: time});
			return res?this.success('变更描述信息成功'):this.fail('编辑信息失败');

		} else {
			let id = this.get('id');
			let info = await this.model('or_rule_admin').where({id: id}).find();
			this.assign('info', info)
			return this.display();
		}
	}
}
